"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sqlitedbService = exports.SqlitedbService = void 0;
const basedb_1 = require("./basedb");
/**
 * sqlite数据存储
 * @class
 */
class SqlitedbService extends basedb_1.BasedbService {
    constructor() {
        const options = {
            dbname: 'sqlite-demo.db',
        };
        super(options);
        Object.defineProperty(this, "userTableName", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.userTableName = 'user';
        this.initTable();
    }
    /*
     * 初始化表
     */
    initTable() {
        // 检查表是否存在
        const masterStmt = this.db.prepare('SELECT * FROM sqlite_master WHERE type=? AND name = ?');
        let tableExists = masterStmt.get('table', this.userTableName);
        if (!tableExists) {
            // 创建表
            const create_user_table_sql = `CREATE TABLE ${this.userTableName}
      (
         id INTEGER PRIMARY KEY AUTOINCREMENT,
         name CHAR(50) NOT NULL,
         age INT
      );`;
            this.db.exec(create_user_table_sql);
        }
    }
    /*
     * 增 Test data (sqlite)
     */
    addTestDataSqlite(data) {
        return __awaiter(this, void 0, void 0, function* () {
            const insert = this.db.prepare(`INSERT INTO ${this.userTableName} (name, age) VALUES (@name, @age)`);
            insert.run(data);
            return true;
        });
    }
    /*
     * 删 Test data (sqlite)
     */
    delTestDataSqlite() {
        return __awaiter(this, arguments, void 0, function* (name = '') {
            const delUser = this.db.prepare(`DELETE FROM ${this.userTableName} WHERE name = ?`);
            delUser.run(name);
            return true;
        });
    }
    /*
     * 改 Test data (sqlite)
     */
    updateTestDataSqlite() {
        return __awaiter(this, arguments, void 0, function* (name = '', age = 0) {
            const updateUser = this.db.prepare(`UPDATE ${this.userTableName} SET age = ? WHERE name = ?`);
            updateUser.run(age, name);
            return true;
        });
    }
    /*
     * 查 Test data (sqlite)
     */
    getTestDataSqlite() {
        return __awaiter(this, arguments, void 0, function* (age = 0) {
            const selectUser = this.db.prepare(`SELECT * FROM ${this.userTableName} WHERE age = @age`);
            const users = selectUser.all({ age: age });
            return users;
        });
    }
    /*
     * all Test data (sqlite)
     */
    getAllTestDataSqlite() {
        return __awaiter(this, void 0, void 0, function* () {
            const selectAllUser = this.db.prepare(`SELECT * FROM ${this.userTableName} `);
            const allUser = selectAllUser.all();
            return allUser;
        });
    }
    /*
     * get data dir (sqlite)
     */
    getDataDir() {
        return __awaiter(this, void 0, void 0, function* () {
            const dir = this.storage.getDbDir();
            return dir;
        });
    }
    /*
     * set custom data dir (sqlite)
     */
    setCustomDataDir(dir) {
        return __awaiter(this, void 0, void 0, function* () {
            if (dir.length == 0) {
                return;
            }
            this.changeDataDir(dir);
            this.initTable();
            return;
        });
    }
}
exports.SqlitedbService = SqlitedbService;
SqlitedbService.toString = () => '[class SqlitedbService]';
const sqlitedbService = new SqlitedbService();
exports.sqlitedbService = sqlitedbService;
